//
//  QuitView.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/14.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "QuitView.h"

@implementation QuitView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        //背景
        UIButton * bgBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
        [bgBtn setBackgroundColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75]];
        [self addSubview:bgBtn];
        //控件搭建
        [self setUpViews];
    }return self;
}
- (void)setUpViews{
    //白色背景
    UIView * whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.cornerRadius = 4;
    whiteView.layer.masksToBounds = YES;
    [self addSubview:whiteView];
    //横线分割线
    UIView * crossLine = [[UIView alloc] init];
    crossLine.backgroundColor = getColor(@"f7f7f7");
    [self addSubview:crossLine];
    //标题
    UILabel * titleLab = [[UILabel alloc] init];
    titleLab.text = @"您确定要退出当前的账号吗?";
    titleLab.font = DEF_FontSize_14;
    titleLab.textColor = [UIColor blackColor];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLab];
    //取消按钮
    UIButton * cancelBtn = [[UIButton alloc] init];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = DEF_FontSize_13;
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    //确定按钮
    UIButton * surelBtn = [[UIButton alloc] init];
    [surelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [surelBtn setTitleColor:getColor(greenBGColor) forState:UIControlStateNormal];
    surelBtn.titleLabel.font = DEF_FontSize_13;
    [surelBtn addTarget:self action:@selector(surelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:surelBtn];
    //竖线
    UIView * verticalLine = [[UIView alloc] init];
    verticalLine.backgroundColor = getColor(@"f7f7f7");
    [self addSubview:verticalLine];
    //masonry
    __weak typeof (self)weakSelf = self;
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).mas_offset(29/WIDTH_6_SCALE);
        make.right.equalTo(weakSelf.mas_right).mas_offset(-29/WIDTH_6_SCALE);
        make.bottom.equalTo(weakSelf.mas_centerY);
        make.height.mas_offset(112/WIDTH_6_SCALE);
    }];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(whiteView);
        make.height.mas_offset(66/WIDTH_6_SCALE);
    }];
    [crossLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLab.mas_bottom);
        make.left.right.equalTo(whiteView);
        make.height.mas_offset(1/WIDTH_6_SCALE);
    }];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(crossLine.mas_bottom);
        make.left.bottom.equalTo(whiteView);
        make.width.equalTo(whiteView.mas_width).multipliedBy(0.5);
    }];
    [surelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(cancelBtn);
        make.right.equalTo(whiteView);
    }];
    [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(crossLine.mas_bottom);
        make.width.mas_offset(1/WIDTH_6_SCALE);
        make.bottom.equalTo(whiteView.mas_bottom);
        make.centerX.equalTo(whiteView.mas_centerX);
    }];
}
#pragma mark - action
- (void)cancelBtnClick{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)surelBtnClick{
    [UserModel sharedInstanced].userID = @"";
    [UserModel sharedInstanced].cookie = @"";
//    [self cancelBtnClick];
    if (self.quitLogBlock) {
        self.quitLogBlock();
    }
//    int64_t delayInSeconds = 10.0;      // 延迟的时间
//    /*
//     *@parameter 1,时间参照，从此刻开始计时
//     *@parameter 2,延时多久，此处为秒级，还有纳秒等。10ull * NSEC_PER_MSEC
//     */
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        // do something
//    });
}
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
@end
